package com.sina.shop.controller.goods;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.goods.Goods;
import com.sina.shop.service.goods.GoodsService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-18:05
 */
@Controller
@RequestMapping("/shop-web/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
    public ModelAndView updateGoods(Goods goods) {
        LOG.info("goods:{}", goods);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsService.updateGoods(goods);
            result.put(CODE, OK);
            result.put(MSG, "更新商品成功");
        } catch (UpdateException e) {
            LOG.info("更新商品出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/getGoods", method = RequestMethod.GET)
    public ModelAndView getGoods(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(6);
        Map<String, Object> data = new HashMap<>(4);
        Goods goods = goodsService.getGoods(id);

        data.put("goods", goods);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    public ModelAndView getGoodsList(Goods goods) {
        LOG.info("goods:{}", goods);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(6);
        Map<String, Object> map = goodsService.getGoodsList(goods);
        result.put(CODE, OK);
        result.put(DATA, map);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveGoods", method = RequestMethod.POST)
    public ModelAndView saveGoods(Goods goods) {
        LOG.info("goods:{}", goods);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsService.saveGoods(goods);
            result.put(CODE, OK);
            result.put(MSG, "新增商品成功");
        } catch (UpdateException e) {
            LOG.info("新增商品出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
    public ModelAndView deleteGoods(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            goodsService.deleteGoods(id);
            result.put(CODE, OK);
            result.put(MSG, "删除商品成功");
        } catch (UpdateException e) {
            LOG.info("删除商品出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
